home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6833 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Turbo C 3.0 - arithmetic in defines
  5. Date: 15 Feb 1996 17:12:26 GMT
  6. Organization: Borland International
  7. Message-ID: <4fvphq$t06@druid.borland.com>
  8. References: <4fqgkf$iqd@ccshst05.cs.uoguelph.ca> <4fre9v$kcs@news.nstn.ca>
  9. NNTP-Posting-Host: pbecker.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4fre9v$kcs@news.nstn.ca>, keichele@ac.dal.ca says...
  15. >
  16. >thay@uoguelph.ca (Toby K Hay) wrote:
  17. >
  18. >>As a newcomer to C programming I'm having difficulty using arithmetic
  19. >>and #defined values to dimension arrays at compile time.  The following 
  20. >>very short program reproduces my error:
  21. >
  22. >>#define var1 16
  23. >>#define var2 485
  24. >>#define var3 ((var2-1)/var1)+1
  25. >>int main(void)
  26. >>       {
  27. >>       printf("\n%i    %i    %i    %i",var1,var2,var3,var1*var3);
  28. >>       return 0;
  29. >>       }
  30. >
  31. >>Output from this is '16 485 31 481', not '16 485 31 496' as I should have 
  32. >>expected.  That is, var3 is calculated correctly in its #define, but the 
  33. >>var1*var3 in the printf() statement is not calculated correctly.  Where 
  34. >>am I going wrong?
  35. >>Toby Hay   thay@uoguelph.ca
  36. >
  37. >Hi,
  38. >you need one more set of parentheses for your definition of var3:
  39.  
  40. Nope. The expression has the right number of parentheses. The problem is 
  41. that they're in the wrong places.
  42.  
  43. >     #define var3 (((var2-1)/var1)+1)
  44.  
  45. #define var3 ((var2-1)/var1+1)
  46.  
  47.